home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Talking KeyBoard / Source / command.c < prev    next >
Text File  |  1998-06-17  |  6KB  |  238 lines

  1. // Program Author: Paul Baxter
  2. //    pbaxter@assistivetech.com
  3. //
  4. //
  5. #include <DeskBus.h>
  6. #include <Retrace.h>
  7. #include <Speech.h>
  8.  
  9. #include "filter.h"
  10. #include "adb.h"
  11. #include "speech.h"
  12. #include "pref.h"
  13. #include "globals.h"
  14. #include "command.h"
  15. #include "menu.h"
  16. #include "aerecord.h"
  17. #include "about.h"
  18.  
  19. #define ACTION_FUNC(cmd)        static void Action##cmd (CommandType command, void* refCon)
  20. #define ACTION_ENTRY(cmd)        cmd, Action##cmd
  21.  
  22. ACTION_FUNC(SpeakCharsCmd);
  23. ACTION_FUNC(SpeakWordsCmd);
  24. ACTION_FUNC(SpeakSentencesCmd);
  25. ACTION_FUNC(QuitCmd);
  26. ACTION_FUNC(VoiceCmd);
  27. ACTION_FUNC(OpenDescAccCmd);
  28. ACTION_FUNC(AboutCmd);
  29.  
  30. static CommandEntry gCommandTable[] = {
  31.     ACTION_ENTRY(SpeakCharsCmd),
  32.     ACTION_ENTRY(SpeakWordsCmd),
  33.     ACTION_ENTRY(SpeakSentencesCmd),
  34.     ACTION_ENTRY(QuitCmd),
  35.     ACTION_ENTRY(VoiceCmd),
  36.     ACTION_ENTRY(OpenDescAccCmd),
  37.     ACTION_ENTRY(AboutCmd)
  38. };
  39. #define kNumCommands sizeof(gCommandTable)/sizeof(CommandEntry)
  40.  
  41.  
  42. static void SetValue(CommandType command, long* var, void* refCon);
  43.  
  44. // * ****************************************************************************** *
  45. // *     ProcessCommand
  46. // *             Process the low level commands of the app
  47. // * ****************************************************************************** *
  48. void ProcessCommand(CommandType command, void* param)
  49. {
  50.     CommandEntry* curcmd;
  51.     short count;
  52.     
  53.     for (count = 0, curcmd = gCommandTable; count < kNumCommands; count++, curcmd++) {
  54.         if ((command & kCommandMask) == (curcmd->command & kCommandMask)) {
  55.             (*curcmd->action)(command, param);
  56.             return;
  57.         }
  58.     }
  59. }
  60.  
  61. // * ****************************************************************************** *
  62. // *     ActionSpeakCharsCmd
  63. // *             Handle a SpeakChars command
  64. // * ****************************************************************************** *
  65. static void ActionSpeakCharsCmd(CommandType command, void* refCon)
  66. {
  67.     MenuHandle menuHandle;
  68.     long oldvalue, newvalue;
  69.     OSErr err;
  70.  
  71.     oldvalue = newvalue = (**gPrefs).speakChars;
  72.     SetValue(command, &newvalue, refCon);
  73.     if (newvalue == oldvalue)
  74.         return;
  75.  
  76.     (**gPrefs).speakChars = newvalue;
  77.     gCharIndex = 0;
  78.  
  79.     menuHandle = GetMenu(kSpeakMenuID);
  80.     CheckItem(menuHandle, kSpeakCharsItem, (**gPrefs).speakChars);
  81.     if ((**gPrefs).speakChars) {
  82.         InstallADBServiceRoutine(gKeyBoardADBAddress);
  83.     }
  84.     else {
  85.         RemoveADBServiceRoutine(gKeyBoardADBAddress);
  86.     }
  87.  
  88.     err = IssueSetSpeakChars();    
  89. }
  90.  
  91.  
  92. // * ****************************************************************************** *
  93. // *     ActionSpeakWordsCmd
  94. // *             Handle a SpeakWords command
  95. // * ****************************************************************************** *
  96. static void ActionSpeakWordsCmd(CommandType command, void* refCon)
  97. {
  98.  
  99.     MenuHandle menuHandle;
  100.     long oldvalue, newvalue;
  101.     OSErr err;
  102.  
  103.     oldvalue = newvalue = (**gPrefs).speakWords;
  104.     SetValue(command, &newvalue, refCon);
  105.     if (newvalue == oldvalue)
  106.         return;
  107.  
  108.     (**gPrefs).speakWords = newvalue;
  109.  
  110.     gWordIndex = 0;
  111.     menuHandle = GetMenu(kSpeakMenuID);
  112.     CheckItem(menuHandle, kSpeakWordsItem, (**gPrefs).speakWords);                    
  113.  
  114.     err = IssueSetSpeakWords();
  115. }
  116.  
  117.  
  118. // * ****************************************************************************** *
  119. // *     ActionSpeakSentencesCmd
  120. // *             Handle a SpeakSentences command
  121. // * ****************************************************************************** *
  122. static void ActionSpeakSentencesCmd(CommandType command, void* refCon)
  123. {
  124.  
  125.     MenuHandle menuHandle;
  126.     long oldvalue, newvalue;
  127.     OSErr err;
  128.  
  129.     oldvalue = newvalue = (**gPrefs).speakSentence;
  130.     SetValue(command, &newvalue, refCon);
  131.     if (newvalue == oldvalue)
  132.         return;
  133.  
  134.     (**gPrefs).speakSentence = newvalue;
  135.     gSentenceIndex = 0;
  136.  
  137.     menuHandle = GetMenu(kSpeakMenuID);
  138.     CheckItem(menuHandle, kSpeakSentenceItem, (**gPrefs).speakSentence);
  139.     err = IssueSetSpeakSentences();
  140. }
  141.  
  142. // * ****************************************************************************** *
  143. // *     ActionQuitCmd
  144. // *             Handle a Quit command
  145. // * ****************************************************************************** *
  146. static void ActionQuitCmd(CommandType command, void* refCon)
  147. {
  148.  
  149.     long oldvalue, newvalue;
  150.  
  151.     oldvalue = newvalue = gQuitting;
  152.     SetValue(command, &newvalue, refCon);
  153.     if (oldvalue != newvalue) {
  154.         gQuitting = (newvalue != 0);
  155.         IssueQuitCommand();
  156.     }
  157. }
  158.  
  159. // * ****************************************************************************** *
  160. // *     ActionVoiceCmd
  161. // *             Handle a change voice command
  162. // * ****************************************************************************** *
  163. static void ActionVoiceCmd(CommandType command, void* refCon)
  164. {
  165.     Str255 oldvalue;
  166.     OSErr err;
  167.  
  168.     BlockMoveData((**gPrefs).voice, oldvalue, sizeof(Str255));
  169.     SetValue(command, (long*)(**gPrefs).voice, refCon);
  170.     if (EqualString((**gPrefs).voice, oldvalue, false, false))
  171.         return;
  172.  
  173.     err = SetDefaultVoice();
  174.     err = IssueSetVoice();
  175. }
  176.  
  177.  
  178. // * ****************************************************************************** *
  179. // *     OpenDescAccCmd
  180. // *             Handle a OpenDeskAcc command
  181. // * ****************************************************************************** *
  182. static void ActionOpenDescAccCmd(CommandType command, void* refCon)
  183. {
  184.     Str255 deskName;
  185.  
  186.     SetValue(command, (long*)deskName, refCon);
  187.     OpenDeskAcc(deskName);
  188.  
  189. }
  190.  
  191. // * ****************************************************************************** *
  192. // *     AboutCmd
  193. // *             Handle a about command
  194. // * ****************************************************************************** *
  195. static void ActionAboutCmd(CommandType command, void* refCon)
  196. {
  197. #pragma unused(command, refCon)
  198.     DoAboutBox();
  199. }
  200.  
  201. // * ****************************************************************************** *
  202. // *     SetValue
  203. // *             change a commands value
  204. // * ****************************************************************************** *
  205. static void SetValue(CommandType command, long* var, void* refCon)
  206. {
  207.     char value;
  208.  
  209.     value = command & kValueMask;
  210.     
  211.     if (value >= kSetValMin && value <= kSetValMax) {
  212.         *var = ((value - kSetValMin)) & kValueMask;
  213.         return;
  214.     }
  215.     else {
  216.         switch (value) {
  217.             case kToggleValue:
  218.                 *var = !(*var);
  219.                 return;
  220.                 
  221.             case kIncValue:
  222.                 (*var)++;
  223.                 return;
  224.  
  225.             case kDecValue:
  226.                 (*var)++;
  227.                 return;
  228.  
  229.             case kStringValue:
  230.                 BlockMoveData(refCon, var, *((char*)refCon) + 1);
  231.                 return;
  232.  
  233.             default:
  234.                 break;
  235.         }
  236.     }
  237. }
  238.